package com.hyl.domain.activity.service.trial.node;

import com.hyl.domain.activity.model.entity.MarketProductEntity;
import com.hyl.domain.activity.model.entity.TrialBalanceEntity;
import com.hyl.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import com.hyl.domain.activity.model.valobj.SkuVO;
import com.hyl.domain.activity.service.discount.IDiscountCalculateService;
import com.hyl.domain.activity.service.trial.AbstractGroupBuyMarketSupport;
import com.hyl.domain.activity.service.trial.factory.DefaultActivityStrategyFactory;
import com.hyl.domain.activity.service.trial.thread.QueryGroupActivityDiscountVOThreadTask;
import com.hyl.domain.activity.service.trial.thread.QuerySkuVOFromDBThreadTask;
import com.hyl.types.design.framework.tree.StrategyHandler;
import com.hyl.types.enums.ResponseCode;
import com.hyl.types.exception.AppException;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;
import java.util.concurrent.*;

/**
 * @author hyl
 * @description营销节点
 * @create 2025-06-15 16:09
 */
@Slf4j
@Service
public class MarketNode extends AbstractGroupBuyMarketSupport<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> {
    @Resource
    private ThreadPoolExecutor threadPoolExecutor;
    @Resource
    private EndNode  endNode;
    @Resource
    private ErrorNode errorNode;
    @Resource
    private TagNode tagNode;
    @Resource
    private Map<String, IDiscountCalculateService> discountCalculateServiceMap;


    @Override
    public TrialBalanceEntity doApply(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        log.info("拼团商品查询试算服务");
        GroupBuyActivityDiscountVO groupBuyActivityDiscountVO = dynamicContext.getGroupBuyActivityDiscountVO();
        if (null == groupBuyActivityDiscountVO){
            return router(requestParameter, dynamicContext);
        }
        GroupBuyActivityDiscountVO.GroupBuyDiscount groupBuyDiscount = groupBuyActivityDiscountVO.getGroupBuyDiscount();
        SkuVO skuVO = dynamicContext.getSkuVO();
        if (null == skuVO){
            return router(requestParameter, dynamicContext);
        }

        IDiscountCalculateService iDiscountCalculateService = discountCalculateServiceMap.get(groupBuyDiscount.getMarketPlan());

        if (null == iDiscountCalculateService){
            throw new AppException(ResponseCode.EOO1.getCode(), ResponseCode.EOO1.getInfo());
        }

        //折扣价格
        BigDecimal payPrice = iDiscountCalculateService.calculate(requestParameter.getUserId(), skuVO.getOriginalPrice(), groupBuyDiscount);
        dynamicContext.setDeductionPrice(skuVO.getOriginalPrice().subtract(payPrice));
        dynamicContext.setPayPrice(payPrice);

        return router(requestParameter, dynamicContext);
    }

    @Override
    public StrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> get(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        if (null == dynamicContext.getGroupBuyActivityDiscountVO()|| null == dynamicContext.getSkuVO()){
            return errorNode;
        }

        return  tagNode;
    }

    @Override
    protected void mutiThread(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws ExecutionException, InterruptedException, TimeoutException {
        // 增加超时时间到2秒
        long extendedTimeout = 2000;
        
        QueryGroupActivityDiscountVOThreadTask queryGroupBuyActivityDiscountVOThreadTask = new QueryGroupActivityDiscountVOThreadTask(repository, requestParameter.getSource(), requestParameter.getChannel(), requestParameter.getGoodsId());
        FutureTask<GroupBuyActivityDiscountVO> groupBuyActivityDiscountVOFutureTask = new FutureTask<>(queryGroupBuyActivityDiscountVOThreadTask);
        threadPoolExecutor.execute(groupBuyActivityDiscountVOFutureTask);

        QuerySkuVOFromDBThreadTask querySkuVOFromDBThreadTask = new QuerySkuVOFromDBThreadTask(requestParameter.getGoodsId(), repository);
        FutureTask<SkuVO> skuVOFutureTask = new FutureTask<>(querySkuVOFromDBThreadTask);
        threadPoolExecutor.execute(skuVOFutureTask);


            GroupBuyActivityDiscountVO discountVO = groupBuyActivityDiscountVOFutureTask.get(extendedTimeout, TimeUnit.MILLISECONDS);
            dynamicContext.setGroupBuyActivityDiscountVO(discountVO);


            SkuVO skuVO = skuVOFutureTask.get(extendedTimeout, TimeUnit.MILLISECONDS);
            dynamicContext.setSkuVO(skuVO);



    }
}
